﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Aliphant.Common;

namespace Aliphant.UI.Assets
{
    /// <summary>
    /// TablePagination.xaml 的交互逻辑
    /// </summary>
    public partial class TablePagination : UserControl
    {
        public TablePagination()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 页码列表
        /// </summary>
        public IEnumerable PageSource
        {
            get { return (IEnumerable)GetValue(PageSourceProperty); }
            set { SetValue(PageSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for PageSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PageSourceProperty =
            DependencyProperty.Register("PageSource", typeof(IEnumerable), typeof(TablePagination), new PropertyMetadata(null));


        public int SelectedIndex
        {
            get { return (int)GetValue(SelectedIndexProperty); }
            set { SetValue(SelectedIndexProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SelectedIndex.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedIndexProperty =
            DependencyProperty.Register("SelectedIndex", typeof(int), typeof(TablePagination));


        /// <summary>
        /// 当前选中项
        /// </summary>
        public object SelectedItem
        {
            get { return (object)GetValue(SelectedItemProperty); }
            set { SetValue(SelectedItemProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SelectedItem.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedItemProperty =
            DependencyProperty.Register("SelectedItem", typeof(object), typeof(TablePagination), new PropertyMetadata(null));


        /// <summary>
        /// 左按钮命令
        /// </summary>
        public ICommand LeftCommand
        {
            get { return (ICommand)GetValue(LeftCommandProperty); }
            set { SetValue(LeftCommandProperty, value); }
        }

        // Using a DependencyProperty as the backing store for LeftCommand.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty LeftCommandProperty =
            DependencyProperty.Register("LeftCommand", typeof(ICommand), typeof(TablePagination), new PropertyMetadata(null));



        /// <summary>
        /// 右按钮命令
        /// </summary>
        public ICommand RightCommand
        {
            get { return (ICommand)GetValue(RightCommandProperty); }
            set { SetValue(RightCommandProperty, value); }
        }

        // Using a DependencyProperty as the backing store for RightCommand.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RightCommandProperty =
            DependencyProperty.Register("RightCommand", typeof(ICommand), typeof(TablePagination), new PropertyMetadata(null));



        /// <summary>
        /// 选择更新命令
        /// </summary>
        public ICommand SelectedChangeCommand
        {
            get { return (ICommand)GetValue(SelectedChangeCommandProperty); }
            set { SetValue(SelectedChangeCommandProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SelectedChangeCommand.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedChangeCommandProperty =
            DependencyProperty.Register("SelectedChangeCommand", typeof(ICommand), typeof(TablePagination), new PropertyMetadata(null));



        /// <summary>
        /// 宽度更新事件
        /// </summary>
        public event RoutedEventHandler WidthChange
        {
            add { AddHandler(WidthChangeEvent, value); }
            remove { RemoveHandler(WidthChangeEvent, value); }
        }

        public static readonly RoutedEvent WidthChangeEvent =
            EventManager.RegisterRoutedEvent("WidthChange", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TablePagination));



        /// <summary>
        /// 宽度更新命令
        /// </summary>
        public ICommand WidthChangeCommand
        {
            get { return (ICommand)GetValue(WidthChangeCommandProperty); }
            set { SetValue(WidthChangeCommandProperty, value); }
        }

        // Using a DependencyProperty as the backing store for WidthChangeCommand.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty WidthChangeCommandProperty =
            DependencyProperty.Register("WidthChangeCommand", typeof(ICommand), typeof(TablePagination), new PropertyMetadata(null));


        private void tabpage_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            var containerWidth = this.ActualWidth;
            var controlWidth = tabBar.ActualWidth + leftBtn.ActualWidth + 40 + rightBtn.ActualWidth + 40;

            WidthChangeCommand?.Execute(containerWidth);
        }
    }
}
